package com.jims.master.modules.la.service;

import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.la.dao.*;
import com.jims.master.modules.la.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by think on 2016/4/28.
 */
@Service
@Transactional(readOnly = true)
public class LaUserService{

    /**
     * 持久层对象
     */
    @Autowired
    protected LaUserMapper userMapper;
    @Autowired
    protected HealthCheckRecordMapper healthCheckRecordMapper;
    @Autowired
    protected BpRecordMapper bpRecordMapper;
    @Autowired
    protected EcgRecordMapper ecgRecordMapper;
    @Autowired
    protected WaterOilRecordMapper waterOilRecordMapper;
    @Autowired
    protected FatRecordMapper fatRecordMapper;
    @Autowired
    protected SugarRecordMapper sugarRecordMapper;
    @Autowired
    protected BreathFreqMapper breathFreqMapper;
    @Autowired
    protected PressureTestRecordMapper pressureTestRecordMapper;

    /**
     * 保存用户数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public String saveUser(User entity) {
        int reslut = 0;
        if(entity != null && StringUtils.isNotEmpty(entity.getUserId())) {
            User user = userMapper.selectByPrimaryKey(entity.getUserId());
        if (user == null ){
            reslut = userMapper.insert(entity);
        }else{
            reslut = userMapper.updateByPrimaryKey(entity);
        }
        }
        return String.valueOf(reslut);
    }

    /**
     * 保存血氧心率体温数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public String saveHealthCheckRecord(HealthCheckRecord entity) {
        int reslut = 0;
        if(entity != null && StringUtils.isNotEmpty(entity.getRecordId())) {
            HealthCheckRecord healthCheckRecord = healthCheckRecordMapper.selectByPrimaryKey(entity.getRecordId());
            if (healthCheckRecord == null ){
                reslut = healthCheckRecordMapper.insert(entity);
            }else{
                reslut = healthCheckRecordMapper.updateByPrimaryKey(entity);
            }
        }
        return String.valueOf(reslut);
    }

    /**
     * 保存血压数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public String saveBpRecord(BpRecordWithBLOBs entity) {
        int reslut = 0;
        if(entity != null && StringUtils.isNotEmpty(entity.getId())) {
            BpRecordWithBLOBs bpRecord = bpRecordMapper.selectByPrimaryKey(entity.getId());
            if (bpRecord == null ){
                reslut = bpRecordMapper.insert(entity);
            }else{
                reslut = bpRecordMapper.updateByPrimaryKey(entity);
            }
        }
        return String.valueOf(reslut);
    }

    /**
     * 保存心电数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public String saveEcgRecord(EcgRecord entity) {
        int reslut = 0;
        if(entity != null && StringUtils.isNotEmpty(entity.getId())) {
            EcgRecord ecgRecord = ecgRecordMapper.selectByPrimaryKey(entity.getId());
            if (ecgRecord == null ){
                reslut = ecgRecordMapper.insert(entity);
            }else{
                reslut = ecgRecordMapper.updateByPrimaryKey(entity);
            }
        }
        return String.valueOf(reslut);
    }

    /**
     * 保存水分油分数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public String saveWaterRecord(WaterOilRecord entity) {
        int reslut = 0;
        if(entity != null && StringUtils.isNotEmpty(entity.getId())) {
            WaterOilRecord waterOilRecord = waterOilRecordMapper.selectByPrimaryKey(entity.getId());
            if (waterOilRecord == null ){
                reslut = waterOilRecordMapper.insert(entity);
            }else{
                reslut = waterOilRecordMapper.updateByPrimaryKey(entity);
            }
        }
        return String.valueOf(reslut);
    }

    /**
     * 保存身体指数数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public String saveFatRecord(FatRecord entity) {
        int reslut = 0;
        if(entity != null && StringUtils.isNotEmpty(entity.getId())) {
            FatRecord fatRecord = fatRecordMapper.selectByPrimaryKey(entity.getId());
            if (fatRecord == null ){
                reslut = fatRecordMapper.insert(entity);
            }else{
                reslut = fatRecordMapper.updateByPrimaryKey(entity);
            }
        }
        return String.valueOf(reslut);
    }

    /**
     * 保存血糖数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public String saveSugarRecord(SugarRecord entity) {
        int reslut = 0;
        if(entity != null && StringUtils.isNotEmpty(entity.getId())) {
            SugarRecord sugarRecord = sugarRecordMapper.selectByPrimaryKey(entity.getId());
            if (sugarRecord == null ){
                reslut = sugarRecordMapper.insert(entity);
            }else{
                reslut = sugarRecordMapper.updateByPrimaryKey(entity);
            }
        }
        return String.valueOf(reslut);
    }

    /**
     * 保存呼吸率数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public String saveBreathFreq(BreathFreq entity) {
        int reslut = 0;
        if(entity != null && StringUtils.isNotEmpty(entity.getId())) {
            BreathFreq breathFreq = breathFreqMapper.selectByPrimaryKey(entity.getId());
            if (breathFreq == null ){
                reslut = breathFreqMapper.insert(entity);
            }else{
                reslut = breathFreqMapper.updateByPrimaryKey(entity);
            }
        }
        return String.valueOf(reslut);
    }

    /**
     * 保存抵抗力、情绪、疲劳指数数据（插入或更新）
     * @param entity
     */
    @Transactional(readOnly = false)
    public String savePressureRecord(PressureTestRecord entity) {
        int reslut = 0;
        if(entity != null && StringUtils.isNotEmpty(entity.getId())) {
            PressureTestRecord pressureTestRecord = pressureTestRecordMapper.selectByPrimaryKey(entity.getId());
            if (pressureTestRecord == null ){
                reslut = pressureTestRecordMapper.insert(entity);
            }else{
                reslut = pressureTestRecordMapper.updateByPrimaryKey(entity);
            }
        }
        return String.valueOf(reslut);
    }


}
